package com.sws.controller;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sws.Constant;
import com.sws.model.bean.B2SPrizePageBean;
import com.sws.model.bean.B2SPrizeReviewBean;
import com.sws.model.bean.C2SPageBean;
import com.sws.model.bean.C2SPrizeBean;
import com.sws.service.PrizeService;
import com.sws.utils.CUtils;

@Controller
public class PrizeController {

	private Logger logger = LoggerFactory.getLogger(PrizeController.class);
	private static final String LOCATION_PATH = "C:/ServerUpload/SWSUpload/image";
	private static final String REQUEST_PATH = "SWSUpload/image/";
//	private static final String LOCATION_PATH = "E:/serverfile/SWSUpload/image";
	
	@Autowired
	private PrizeService service;
	
	@ResponseBody
	@RequestMapping("/s/uploadPrize")
	public String uploadPrize(C2SPrizeBean prize, HttpServletRequest request) {
		JSONObject result = new JSONObject();
		logger.info("=====> 上传获奖信息及证明");
		String fileName = "prize_" + CUtils.getDateTime() + "_" + prize.getFile().getOriginalFilename();
		File dir = new File(LOCATION_PATH);
		//判断上传文件的保存目录是否存在
		if (!dir.exists() && !dir.isDirectory()) {
			logger.info("创建目录");
			dir.mkdirs();
		}
		try {
			File saveFile = new File(LOCATION_PATH, fileName);
			prize.getFile().transferTo(saveFile);
			prize.setDocument(REQUEST_PATH+fileName);
			service.uploadPrize(prize);
			if (prize.getIsSuccess()) {
				result.put("status", Constant.INSTANSE.OK);
				result.put("msg", "上传成功！");
			} else {
				result.put("status", Constant.INSTANSE.FAIL);
				result.put("msg", "上传失败，请检查！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/s/getPersonalPrize")
	public String getPersonalPrize(C2SPageBean pageBean) {
		logger.info("=====> 获取个人获奖信息");
		JSONObject result = new JSONObject();
		
		try {
			result.put("status", Constant.INSTANSE.OK);
			result.put("data", service.getPersonalPrize(pageBean));
			result.put("msg", "获取个人获奖信息成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/t/prizeReview")
	public String prizeReview(B2SPrizeReviewBean bean) {
		logger.info("=====> 审核获奖信息");
		JSONObject result = new JSONObject();

		try {
			service.prizeReview(bean);
			result.put("status", Constant.INSTANSE.OK);
			result.put("msg", "审核获奖信息成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/t/getAllPrize")
	public String getAllPrize(B2SPrizePageBean bean) {
		logger.info("=====> 获取所有获奖信息");
		JSONObject result = new JSONObject();
		
		try {
			result.put("status", Constant.INSTANSE.OK);
			result.put("data", service.getAllPrize(bean));
			result.put("msg", "获取所有获奖信息成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
}
